﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DriveBeamEditor.Properties;
using NativeLibrary;

namespace DriveBeamEditor.Controls.BeamNG
{
    public partial class BeamNGForm : Form
    {
        #region Fields

        private Color _backColor2 = Color.Gainsboro;

        #endregion

        #region Properties

        public Color BackColor2
        {
            get { return _backColor2; }
            set
            {
                _backColor2 = value;
                Invalidate();
            }
        }

        private RectangleF MainArea
        {
            get
            {
                return new RectangleF(
                    Padding.Left,
                    Padding.Top + Resources.beamng.Height,
                    Width - Padding.Left - Padding.Right - 1,
                    Height - Padding.Top - Padding.Bottom - Resources.beamng.Height * 2 - 1
                );
            }
        }

        public override string Text
        {
            get { return base.Text; }
            set
            {
                base.Text = value;
                Invalidate();
            }
        }

        #endregion

        #region CTORs

        public BeamNGForm()
        {
            InitializeComponent();
            SetStyle(
                ControlStyles.AllPaintingInWmPaint |
                ControlStyles.DoubleBuffer |
                ControlStyles.OptimizedDoubleBuffer |
                ControlStyles.ResizeRedraw |
                ControlStyles.CacheText |
                ControlStyles.SupportsTransparentBackColor |
                ControlStyles.UserPaint,
                true
            );
        }

        #endregion

        #region Events

        private void xbClose_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            if (!Modal) Close();
        }

        private void xbMinimize_Click(object sender, EventArgs e)
        {
            WindowState = FormWindowState.Minimized;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            var rect = new RectangleF(0, Resources.beamng.Height, Width, Height - Resources.beamng.Height);
            var botRect = new RectangleF(0, Height - Resources.beamng.Height, Width, Resources.beamng.Height);
            var topRect = new RectangleF(0, 0, Width, Resources.beamng.Height + 1);
            var bordRect = new RectangleF(
                Padding.Left,
                Padding.Top + Resources.beamng.Height,
                Width - Padding.Left - Padding.Right - 1,
                Height - Padding.Top - Padding.Bottom - Resources.beamng.Height * 2 - 1
            );
            var tSize = e.Graphics.MeasureString(Text, Font);
            var sColor = Color.FromArgb(120, 255 - ForeColor.R, 255 - ForeColor.G, 255 - ForeColor.B);

            e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
            e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            e.Graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
            e.Graphics.FillRectangle(new LinearGradientBrush(rect, BackColor2, BackColor, LinearGradientMode.Vertical), rect);
            e.Graphics.FillRectangle(new LinearGradientBrush(botRect, BackColor2, Color.FromArgb(255, 102, 0), LinearGradientMode.Vertical), botRect);
            e.Graphics.FillRectangle(new LinearGradientBrush(topRect, Color.FromArgb(150, 255, 102, 0), Color.Transparent, LinearGradientMode.Vertical), topRect);
            e.Graphics.DrawString(Text, Font, new SolidBrush(sColor), Resources.beamng.Width + 2f, topRect.Height / 2f - tSize.Height / 2f + 1f);
            e.Graphics.DrawString(Text, Font, new SolidBrush(ForeColor), Resources.beamng.Width, topRect.Height / 2f - tSize.Height / 2f);
            e.Graphics.DrawRectangle(new Pen(Color.FromArgb(255, 108, 0)), bordRect.X, bordRect.Y, bordRect.Width, bordRect.Height);

            base.OnPaint(e);
        }

        protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
                case (int)WindowsMessages.WM_NCHITTEST:
                    base.WndProc(ref m);
                    if ((int)m.Result == (int)HitTestResults.HTCLIENT)
                    {
                        var cur = PointToClient(new Point(HitTest.GetX(m.LParam), HitTest.GetY(m.LParam)));
                        var mainArea = MainArea;
                        if (cur.X <= mainArea.Left && cur.Y >= mainArea.Top && cur.Y <= mainArea.Bottom)
                            m.Result = (IntPtr)HitTestResults.HTLEFT;
                        else if (cur.X >= mainArea.Right && cur.Y >= mainArea.Top && cur.Y <= mainArea.Bottom)
                            m.Result = (IntPtr)HitTestResults.HTRIGHT;
                        else if (cur.Y >= mainArea.Bottom && cur.X >= mainArea.Left && cur.X <= mainArea.Right)
                            m.Result = (IntPtr)HitTestResults.HTBOTTOM;
                        else if (cur.X <= mainArea.Left && cur.Y < mainArea.Top)
                            m.Result = (IntPtr)HitTestResults.HTTOPLEFT;
                        else if (cur.X >= mainArea.Right && cur.Y < mainArea.Top)
                            m.Result = (IntPtr)HitTestResults.HTTOPRIGHT;
                        else if (cur.X <= mainArea.Left && cur.Y > mainArea.Bottom)
                            m.Result = (IntPtr)HitTestResults.HTBOTTOMLEFT;
                        else if (cur.X >= mainArea.Right && cur.Y > mainArea.Bottom)
                            m.Result = (IntPtr)HitTestResults.HTBOTTOMRIGHT;
                        else
                            m.Result = (IntPtr)HitTestResults.HTCAPTION;
                    }
                    return;
            }

            base.WndProc(ref m);
        }

        #endregion
    }
}
